package com.nisco.ampf.util;

import java.util.ArrayList;
import java.util.HashMap;

import android.util.Log;

public class DateSwitch {

	/**
	 * 工具类 功能：从webserver返回的string数组重新组装为ArrayList型
	 * 
	 * @param s
	 * @return
	 */
	public ArrayList<HashMap<String, Object>> stringToMap(String[] s) {
		ArrayList<HashMap<String, Object>> List = new ArrayList<HashMap<String, Object>>();
		if (s == null) {
			return null;
		} else {
			for (int i = 0; i < s.length; i++) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				String[] list = s[i].split(";");
				for (int j = 0; j < list.length - 1; j++) {
					String[] subList = list[j].split("=");
					map.put(subList[0].trim(), subList[1]);
				}
				List.add(map);
			}

			return List;
		}
	}

	/**
	 * 将arrayList转换为string类型,例如："P1T0103,11300209030204;P1T0103,11300209030205"
	 * 作为倒垛时的参数
	 * 
	 * @param arrayList
	 * @return
	 */
	public String arrayListToString(ArrayList<HashMap<String, Object>> arrayList,String filedName) {
		StringBuffer buffer = new StringBuffer();
		if (arrayList == null) {
			return null;
		} else {
			// 选择一项的处理情况
			if (arrayList.size() == 1) {
				buffer.append(arrayList.get(0).get(ConstString.YARD_ADDR)
						.toString()
						+ ","
						+ arrayList.get(0).get(filedName).toString());
			} else {
				// 选择多项的处理情况
				for (int i = 0; i < arrayList.size() - 1; i++) {
					buffer.append(arrayList.get(i).get(ConstString.YARD_ADDR)
							.toString()
							+ ","
							+ arrayList.get(i).get(filedName)
									.toString() + ";");
					
				}
				buffer.append(arrayList.get(arrayList.size() - 1)
						.get(ConstString.YARD_ADDR).toString()
						+ ","
						+ arrayList.get(arrayList.size() - 1)
								.get(filedName).toString());
				Log.i("io", buffer.toString());
			}
			return buffer.toString();
		}

	}
}
